package com.example.shoppingmallspringboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.shoppingmallspringboot.entity.User;
import com.example.shoppingmallspringboot.mapper.UserMapper;
import com.example.shoppingmallspringboot.service.UserService;
import com.example.shoppingmallspringboot.util.FwResult;
import com.example.shoppingmallspringboot.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserMapper userMapper;
    
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public FwResult login(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUsername());
        List<User> userList = userMapper.selectList(wrapper);
        
        if (userList.size() == 0) {
            return FwResult.okMsg("用户名不存在");
        }
        
        String password = user.getPassword();
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        
        if (userList.get(0).getPassword().equals(md5Password)) {
            String token = JwtUtil.createToken();
            redisTemplate.opsForValue().set(token, userList.get(0), 1440, TimeUnit.MINUTES);
            
            Map<String, String> map = new HashMap<>();
            map.put("token", token);
            map.put("username", user.getUsername());
            map.put("password", String.valueOf(user.getPassword()));
            map.put("profile", userList.get(0).getProfile());
            map.put("userId", String.valueOf(userList.get(0).getId()));
            
            return FwResult.ok(map, "登录成功");
        } else {
            return FwResult.okMsg("密码错误");
        }
    }

    @Override
    public FwResult register(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUsername());
        List<User> userList = userMapper.selectList(wrapper);
        
        if (userList.size() > 0) {
            return FwResult.okMsg("用户名已存在");
        }
        
        String password = user.getPassword();
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(md5Password);
        userMapper.insert(user);
        return FwResult.ok("注册成功");
    }

    @Override
    public FwResult updateUser(User user) {
        User updateUser = new User();
        String password = user.getPassword();
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        updateUser.setId(user.getId());
        updateUser.setUsername(user.getUsername());
        updateUser.setPassword(md5Password);
        userMapper.updateById(updateUser);
        return FwResult.ok(user, "修改成功");
    }
} 